//
//  TKIMRefreshHeaderView.m
//  TKIM_首创
//
//  Created by macpro on 15/12/10.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMRefreshHeaderView.h"
//加载更多的视图默认高度
#define kRefreshHeaderViewHeight 40

//动画时间
#define kAnimationDuration 0.3


@interface TKIMRefreshHeaderView()<UIScrollViewDelegate>
{
//    UIScrollView *_scrollView;
    //加载更多回调
    void(^_beginLoadMoreBlock)(TKIMRefreshHeaderView *);
    
}
@property(nonatomic,weak)UIScrollView * scrollView;
/**
 *  加载提示
 */
@property (nonatomic, weak) UIActivityIndicatorView *indicatorView;

/**
 *  提示文字
 */
@property (nonatomic, weak) UILabel *tipLabel;
/**
 *  无更多数据
 */
@property (nonatomic, weak) UILabel *noDataLabel;
@end


@implementation TKIMRefreshHeaderView


#pragma mark - 懒加载创建控件

- (UIActivityIndicatorView *)indicatorView
{
    if (!_indicatorView)
    {
        UIActivityIndicatorView *act = [[UIActivityIndicatorView alloc] init];
        act.color = [UIColor grayColor];
        [self addSubview:act];
        _indicatorView = act;
    }
    
    return _indicatorView;
}

- (UILabel *)tipLabel
{
    if (!_tipLabel)
    {
        UILabel *lb = [[UILabel alloc] init];
        lb.textAlignment = NSTextAlignmentLeft;
        lb.textColor = [UIColor grayColor];
        lb.font = [UIFont systemFontOfSize:13];
        lb.text = @"数据加载中...";
        [self addSubview:lb];
        _tipLabel = lb;
    }
    
    return _tipLabel;
}

- (UILabel *)noDataLabel
{
    if (!_noDataLabel)
    {
        UILabel *lb = [[UILabel alloc] init];
        lb.textAlignment = NSTextAlignmentCenter;
        lb.textColor = [UIColor grayColor];
        lb.font = [UIFont systemFontOfSize:13];
        lb.text = @"无更多历史数据";
        [self addSubview:lb];
        _noDataLabel = lb;
    }
    
    return _noDataLabel;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    CGSize size=[self.tipLabel sizeThatFits:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    self.tipLabel.frame = CGRectMake((self.width-size.width)*0.5+20, 0, size.width, self.height);
    self.indicatorView.frame = CGRectMake(self.tipLabel.left - 40, 0, self.height, self.height);
    self.noDataLabel.frame = CGRectMake(0, 0, self.width, self.height);
}

#pragma mark - init方法
- (instancetype)initWithScrollView:(UIScrollView *)scrollView beginLoadMoreBlock:(void (^)(TKIMRefreshHeaderView *))beginLoadMoreBlock
{
    if (self = [super initWithFrame:CGRectMake(0,-kRefreshHeaderViewHeight, scrollView.frame.size.width, kRefreshHeaderViewHeight)])
    {
        _scrollView = scrollView;
        _beginLoadMoreBlock = beginLoadMoreBlock;
        self.haveMoreHistoryData = YES;
        //把当前视图加到uiscrollView
        [_scrollView addSubview:self];
        //设置背景颜色
        self.backgroundColor = [UIColor clearColor];
        [self indicatorView];
        [self tipLabel];
        //注册观察者
        [_scrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    }
    
    return self;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentOffset"])
    {
        if (_scrollView.contentOffset.y<0&&![self.indicatorView isAnimating]&&_scrollView.dragging) {
            if (self.haveMoreHistoryData) {
                [self.indicatorView startAnimating];
                self.tipLabel.text = @"加载中...";
                _scrollView.contentInset = UIEdgeInsetsMake(kRefreshHeaderViewHeight, 0, 0, 0);
                if (_beginLoadMoreBlock) {
                    _beginLoadMoreBlock(self);
                }
                self.noDataLabel.hidden = YES;
            }else{
                self.noDataLabel.hidden = NO;
            }
        }
    }
}


/**
 *  停止刷新
 */
- (void)endRefresh
{
    [UIView  animateWithDuration:kAnimationDuration animations:^{
        _scrollView.contentInset = UIEdgeInsetsZero;
    } completion:^(BOOL finished) {
        [self.indicatorView stopAnimating];
        self.tipLabel.text = @"";
        
        self.noDataLabel.hidden = NO;
    }];
}

-(void)dealloc
{
    [_scrollView removeObserver:self forKeyPath:@"contentOffset"];
}

@end
